package me.ezitku.base.extensions

import android.app.Activity
import androidx.fragment.app.Fragment
import android.text.InputType
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import com.daimajia.androidanimations.library.Techniques
import com.ug_project.views.checkEmptyEditTexts
import com.ug_project.views.childAll
import com.ug_project.views.content
import com.ug_project.views.isEmpty

/**
 * Created by yurqi on 4/16/2017.
 */
fun EditText.clear() {
    this.content = ""
}

fun EditText.inputTypeNull() {
    inputType = InputType.TYPE_NULL
}

fun EditText.focused(action: () -> Unit) {
    focusChangeListener {
        focuChange {
            if (it)
                action()
        }
    }
}

fun EditText.focusChangeListener(action: focusChangeListener.() -> Unit) {
    onFocusChangeListener = focusChangeListener().apply(action)
}


class focusChangeListener : View.OnFocusChangeListener {
    var _focusChange: ((hasFocus: Boolean) -> Unit)? = null
    override fun onFocusChange(v: View?, hasFocus: Boolean) {
        _focusChange?.invoke(hasFocus)
    }

    fun focuChange(action: (hasFocus: Boolean) -> Unit) {
        _focusChange = action
    }
}

/**
 * ئىككى پارولنىڭ بىردەك ياكى ئەمەسلىكىنى تەكشۈرۈش
 */
fun EditText.comparePwd(pwd: EditText, action: () -> Unit, err: ((edit1: EditText, edit2: EditText) -> Unit)? = null) {
    if (this.content.trim().contentEquals(pwd.content.trim())) {
        action()
        return
    }
    if (err == null) {
        this.anim = Techniques.Swing
        pwd.anim = Techniques.Swing
        return
    }
    err(this, pwd)
}


//نۇر بەلگىنى ئەڭ ئاخىرغا يۆتكەپ بېرىدۇ
fun EditText.selectionEnd() {
    this.setSelection(this.text.toString().length)
}


// =========================  CheckEdit =====================

/**
 * "نۆۋەتتىكى Activity نىڭ 布局 نىڭ ئەڭ چوڭ دادا Layout"
 */
private val Activity.contentView: ViewGroup
    get() = (findViewById(android.R.id.content))

/**
 * نۆۋەتتىكى Activity نىڭ ئىچىدىكى بارلىق EditText لارنىڭ قىممىتى قۇرۇقدايدۇ
 */
fun Activity.clearUiInAllEdits() {
    contentView.clearAllEdits()
}

fun Fragment.clearUiInAllEdits() {
    activity!!.clearUiInAllEdits()
}

fun ViewGroup.clearAllEdits() {
    childAll.filter { it is EditText }.forEach { (it as EditText).content = "" }
}

fun Array<EditText>.clearEdits() {
    this.forEach {
        it.content = ""
    }
}

/**
 * "مۇشۇ Activity نىڭ ئىچىدىكى بارلىق EditText لارنى ئىزدەپ قۇرۇق EditText بايقالسا شۇنىڭغا فوكۇسلاپ بېرىدۇ"
 */
fun Activity.checkUiInAllEdits(action: () -> Unit) {
    contentView.checkEdits(action)
}

/**
 * "مۇشۇ Fragment نىڭ ئىچىدىكى بارلىق EditText لارنى ئىزدەپ قۇرۇق EditText بايقالسا شۇنىڭغا فوكۇسلاپ بېرىدۇ"
 */
fun Fragment.checkUiInAllEdits(action: () -> Unit) {
    activity!!.checkUiInAllEdits(action)
}

/**
 * "مۇشۇ ViewGroup نىڭ ئىچىدىكى بارلىق EditText لارنى ئىزدەپ قۇرۇق EditText بايقالسا شۇنىڭغا فوكۇسلاپ بېرىدۇ"
 */
fun ViewGroup.checkEdits(action: () -> Unit) {
    if (this.checkEmptyEditTexts(::handleEmptyEdit)) {
        action()
    }
}

/**
 * "مۇشۇ Array نىڭ ئىچىدىكى بارلىق EditText لارنى ئىزدەپ قۇرۇق EditText بايقالسا شۇنىڭغا فوكۇسلاپ بېرىدۇ"
 */
fun Array<EditText>.checkEdits(action: () -> Unit) {
    forEach {
        if (it.isEmpty) {
            handleEmptyEdit(it)
            return
        }
    }
    action()
}

/**
 * "مۇشۇ EditText نىڭ قىممىتى قۇرۇق بوپ قالسا فوكۇسلاپ بېرىدۇ"
 */
fun EditText.checkEdit(action: (EditText) -> Unit) {
    if (this.isEmpty) {
        handleEmptyEdit(this)
        return
    }
    action(this)
}


private fun handleEmptyEdit(edit: EditText) {
    edit.requestFocus()
    edit.showKeyBoard()
    edit.errAnim()
//    edit.error = edit.hint
}

/**
 * ئەگەر قۇرۇق بولسا ياكى ئايلاندۇرالمىسا 0 قايتۇرىدۇ
 */
fun TextView.toInt(): Int {
    return try {
        content.toInt()
    } catch (e: Exception) {
        0
    }
}

fun TextView.toDouble(): Double {
    return try {
        content.toDouble()
    } catch (e: Exception) {
        0.0
    }
}

fun TextView.toFloat(): Float {
    return try {
        content.toFloat()
    } catch (e: Exception) {
        0f
    }
}

fun TextView.format_price(decimal: Int = 2) {
    content = "${toDouble().format(decimal)}"
}


fun EditText.actions(action: EditTextEditorAction.() -> Unit) {
    val model = EditTextEditorAction()
    action(model)
    setOnEditorActionListener { v, actionId, event ->
        if (actionId == EditorInfo.IME_ACTION_SEND
                || (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER)) {
            model._enter(v)
        }
        model._action(v, actionId, event)
        return@setOnEditorActionListener true
    }
}

class EditTextEditorAction {
    var _enter: (tv: TextView) -> Unit = {}
    var _action: (tv: TextView, actionId: Int, event: KeyEvent) -> Unit = { tv, id, evet -> }
    fun enterAction(action: (tv: TextView) -> Unit) {
        _enter = action
    }

    fun customAction(action: (tv: TextView, actionId: Int, event: KeyEvent) -> Unit) {
        _action = action
    }


}